
#include "protocol.h"
#include "uart.h"
#include "led.h"
	
static uint8_t ackFlag   = 0;	
static uint8_t addressId = 0;	
static uint8_t addressEndFlag=0;
	
uint8_t sendAddressTrans[29]={0xfd,0xfd,0x19,0x01};

uint8_t sendAddressAck[29]	={0xfd,0xfd,0x19,0x10,\
	0xef,0x01,0x00,0x00,0xef,0x02,0x00,0x00,0xef,0x03,0x00,0x00,\
	0xef,0x04,0x00,0x00,0xef,0x05,0x00,0x00,0xef,0x06,0x00,0x00,\
	0x00};

uint8_t sendLedTrans[29]={0xfd,0xfd,0x19,0x02};
uint8_t sendTransaUpdata[29]={0xfd,0xfd,0x19,0x03,\
	0xef,0x01,0x00,0x00,0xef,0x02,0x00,0x00,0xef,0x03,0x00,0x00,\
	0xef,0x04,0x00,0x00,0xef,0x05,0x00,0x00,0xef,0x06,0x00,0x00,\
	0x00};



void hc32f4dataHandle(uint8_t cmd, uint8_t *data)
{
	switch (cmd)
	{
		case 0:
		break;
		case 1:
		break;
		case 2:
		break;
		case 3:
		{
			uint8_t senddatatmp[24];//去掉cmd 一共收到24个数据位
			for(uint8_t j=0;j<24;j++)
			{
				sendTransaUpdata[j+4]=data[j];
			}
			if(addressId>=1)
			{
				//sendTransaUpdata[(addressId-1)*4+1+4]=height;
			}
			sendTransaUpdata[1]=0xfd;
			//uartSendData(sendTransaUpdata,29);
		}
		break; 
		case 0x10://判断是否收到地址回传
		{
			addressEndFlag=1;
			//uartSendData(sendAddressAck,29);
		}
		break; 
		default:
		break;
    }
}
	
void bl602DataHandle(uint8_t cmd, uint8_t *data)
{
	switch (cmd)
	{
		case 0:
		{
 
		}
		break;
		case 1:
		{
			for(uint8_t i=0;i<=5;i++)
			{
				if(data[4*i]==0xef)
				{
					uint8_t tmp_adress=data[4*i+1];
					if((tmp_adress==0x01)||\
					(tmp_adress==0x02)||\
					(tmp_adress==0x03)||\
					(tmp_adress==0x04)||\
					(tmp_adress==0x05)||\
					(tmp_adress==0x06))
					{
						addressId=tmp_adress;
						data[4*i+1]=0;
						for(uint8_t j=0;j<25;j++)
						{
							sendAddressTrans[j+4]=data[j];
						}
//						usartMicrobitSend(sendAddressTrans,29);
						//uartSendData(sendAddressAck,29);
						break;
					}
				}
			}
		}
		break;
		case 2:
			if(data[4*(addressId-1)]==0xef)
			{
				uint8_t led_red=data[4*(addressId-1)+1];
				if(led_red==0xff)
				{
					//ledSingleOn();	 
				}
				else
				{
					//ledSingleOff();
				}
				
				for(uint8_t j=0;j<25;j++)
				{
					sendLedTrans[j+4]=data[j];
				}
				//uartSendData(sendLedTrans,29);
//				usartMicrobitSend(sendLedTrans,29);
			}
		break;
		case 3:
		{	
			uint8_t senddatatmp[24];//去掉cmd 一共收到24个数据位
			for(uint8_t j=0;j<=23;j++)
			{
				sendTransaUpdata[j+4]=data[j];
			}
			//sendTransaUpdata[(addressId-1)*4+1+4]=height;
//			usartMicrobitSend(sendTransaUpdata,29);
		}	 
		break;
		case 4:
		{
			uint8_t time = data[0];
			uint8_t rec[2] = {0, cmd};
			if (time < 1)
			{
				time = 1;
			}
			receiveData(cmd, 2, rec);
		}
		break;
		case 5:
		{
			//uint16_t h = getHeight();
			//uint8_t rec[2] = {h, h >> 8};
			//receiveData(cmd, 2, rec);
		}
		break;
		default:
		break;
    }
}

void receiveData(uint8_t cmd, uint8_t len, uint8_t *data)
{
    uint8_t rec[16] = {0xFB, 0xFB, len, cmd};
    uint8_t i;
    uint8_t sum = len + cmd;
    for (i = 0; i < len; i++)
    {
        rec[4 + i] = data[i];
        sum += data[i];
    }
    rec[4 + i] = sum;
    //uartSendData(rec, len + 5);
}

void uartSendUpdate(void)
{
	if(addressId==0x02)//||(addressEndFlag==0)没收到下一个设备回传的时候是最后一个设备,第六个一定是最后一个设备
	{
		if(addressId>=1)
		{
			//sendTransaUpdata[(addressId-1)*4+1+4]=height;
			
			//sendTransaUpdata[(addressId-1)*4+1+4+1]=shake;
			//sendTransaUpdata[(addressId-1)*4+1+4+2]=voltage;
		}
		//uartSendData(sendTransaUpdata,29);
	}	
	else if(addressId==0)//||(addressId==1))
	{
		//sendTransaUpdata[1+4]=height; 
		//uartSendData(sendTransaUpdata,29);
	}
}
